﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Core;
using System.Collections;
using System.Reflection;
using System.Collections.ObjectModel;

namespace Portal.Web.Utility
{
    public class PlugInViewUtility
    {
        private static IPluginView[] LoadAssembly(string filename)
        {
            ArrayList ArrayListModules = new ArrayList();
            Assembly a = Assembly.LoadFrom(filename);
            if (a == null) throw new Exception("Assembly " + filename + " not found or not valid");
            foreach (Module mod in a.GetLoadedModules())
            {
                foreach (Type ty in mod.GetTypes())
                {
                    foreach (Type intf in ty.GetInterfaces())
                    {
                        if (intf == typeof(IPluginView))
                        {
                            IPluginView w = (IPluginView)a.CreateInstance(ty.FullName);
                            ArrayListModules.Add(w);
                        }
                    }
                }
            }
            IPluginView[] ws = new IPluginView[ArrayListModules.Count];
            for (int iIndex = 0; iIndex < ArrayListModules.Count; iIndex++)
            {
                ws[iIndex] = (IPluginView)ArrayListModules[iIndex];
            }
            return ws;
        }


        public static IPluginView[] LoadPlugIn(string SiteId)
        {
            SiteParam oSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string Path = oSiteParam.DllPath;
            string[] Files = System.IO.Directory.GetFiles(Path, "*.dll");
            ArrayList ArrayListServives = new ArrayList();
            SiteParam OSiteParam=WebEnvironments.CreateSiteParam(SiteId);
            string WebPlugInViewDll = (string)WebConfig.GetConfigFromXml(oSiteParam, "WebPlugInViewDll");
            string[] ItemsWebPlugInViewIds = WebPlugInViewDll.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            for (int iIndex = 0; iIndex < Files.Length; iIndex++)
            {
                try
                {
                    string FileDll = Files[iIndex];
                    bool Allow = false;
                    for (int iCheck = 0; iCheck < ItemsWebPlugInViewIds.Length; iCheck++)
                    {
                        if (new System.IO.FileInfo(FileDll).Name.Trim().ToLower().Equals(ItemsWebPlugInViewIds[iCheck].Trim().ToLower()))
                        {
                            Allow = true;
                            break;
                        }
                    }
                    if(Allow)
                    {
                        IPluginView[]
                            asms = LoadAssembly(FileDll);
                        for (int jIndex = 0; jIndex < asms.Length; jIndex++)
                        {
                            ArrayListServives.Add(asms[jIndex]);
                        }
                    }
                }
                catch (Exception ex)
                {
                    ex.Source = "";
                }
            }
            IPluginView[] WebParts = new IPluginView[ArrayListServives.Count];
            for (int iIndex = 0; iIndex < ArrayListServives.Count; iIndex++)
            {
                WebParts[iIndex] = (IPluginView)ArrayListServives[iIndex];
            }

            return WebParts;
        }


        public static Collection<IPluginView> Find(string SiteId, string PlugInWebPartId)
        {
            Collection<IPluginView> IPluginViews = new Collection<IPluginView> { };
            IPluginView[] found = LoadPlugIn(SiteId);
            for (int iIndex = 0; iIndex < found.Length; iIndex++)
            {
                if (found[iIndex].PlugInWebPartId != null)
                {
                    if (found[iIndex].PlugInWebPartId.Trim().ToLower().Equals(PlugInWebPartId.Trim().ToLower()))
                    {
                        IPluginViews.Add(found[iIndex]);
                    }
                }
            }
            return IPluginViews;
        }


        public static IPluginView FindOnePlugInView(string SiteId, string PlugInWebPartId,string PlugInViewId)
        {
            IPluginView[] found = LoadPlugIn(SiteId);
            for (int iIndex = 0; iIndex < found.Length; iIndex++)
            {
                if (found[iIndex].PlugInWebPartId != null)
                {
                    if (found[iIndex].PlugInWebPartId.Trim().ToLower().Equals(PlugInWebPartId.Trim().ToLower()) && found[iIndex].ServiceId.Trim().ToLower().Equals(PlugInViewId.Trim().ToLower()))
                    {
                        return found[iIndex];
                    }
                }
            }
            return null;
        }
    }
}
